package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map;
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.opensymphony.xwork2.ActionContext;
import java.util.Date;
import com.lb.jwitter.util.ApplicationContextProvider;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;
import com.lb.jwitter.service.TblCTDTDMHocKyTrongNam;
import com.lb.jwitter.service.TblCTDTDMHocKyTrongNamService;
import com.lb.jwitter.service.TblDKMHKetquaService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

import com.lb.jwitter.service.TblDKMHAAOUrl;
import com.lb.jwitter.service.TblDKMHAAOUrlService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblDKMHDotDKInsertAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private Long id;

	public void setId(Long id) {
		this.id = id;
	}

	private String ma;

	public void setMa(String ma) {
		this.ma = ma;
	}

	public String getMa() {
		return ma;
	}

	private String ten;

	public void setTen(String ten) {
		this.ten = ten;
	}

	public String getTen() {
		return ten;
	}

	private String maLoai;

	public void setMaLoai(String maLoai) {
		this.maLoai = maLoai;
	}

	public String getMaLoai() {
		return maLoai;
	}

	private int hocKyId;

	public void setHocKyId(int hocKyId) {
		this.hocKyId = hocKyId;
	}

	public int getHocKyId() {
		return hocKyId;
	}

	private Collection<TblCTDTDMHocKyTrongNam> tblCTDTDMHocKyTrongNams;

	public Collection<TblCTDTDMHocKyTrongNam> getTblCTDTDMHocKyTrongNams() {
		return tblCTDTDMHocKyTrongNams;
	}

	private TblCTDTDMHocKyTrongNamService tblCTDTDMHocKyTrongNamService;

	private String ngayBatDau;

	public void setNgayBatDau(String ngayBatDau) {
		this.ngayBatDau = ngayBatDau;
	}

	public String getNgayBatDau() {
		return ngayBatDau;
	}

	private String ngayKetThuc;

	public void setNgayKetThuc(String ngayKetThuc) {
		this.ngayKetThuc = ngayKetThuc;
	}

	public String getNgayKetThuc() {
		return ngayKetThuc;
	}

	private String ngayCapNhat;

	public void setNgayCapNhat(String ngayCapNhat) {
		this.ngayCapNhat = ngayCapNhat;
	}

	public String getNgayCapNhat() {
		return ngayCapNhat;
	}

	private String ghiChu;

	public void setGhiChu(String ghiChu) {
		this.ghiChu = ghiChu;
	}

	public String getGhiChu() {
		return ghiChu;
	}

	private int trangThai;

	public void setTrangThai(int trangThai) {
		this.trangThai = trangThai;
	}

	public int getTrangThai() {
		return trangThai;
	}

	private int canBoId;

	public void setCanBoId(int canBoId) {
		this.canBoId = canBoId;
	}

	public int getCanBoId() {
		return canBoId;
	}

	private Collection<TblCanBo> tblCanBos;

	public Collection<TblCanBo> getTblCanBos() {
		return tblCanBos;
	}

	private TblCanBoService tblCanBoService;

	private int dotDKTruocId;

	public void setDotDKTruocId(int dotDKTruocId) {
		this.dotDKTruocId = dotDKTruocId;
	}

	public int getDotDKTruocId() {
		return dotDKTruocId;
	}

	private Collection<TblDKMHDotDK> tblDKMHDotDKs;

	public Collection<TblDKMHDotDK> getTblDKMHDotDKs() {
		return tblDKMHDotDKs;
	}

	private TblDKMHDotDKService tblDKMHDotDKService;

	private TblDKMHDotDK tblDKMHDotDK;

	public TblDKMHDotDK getTblDKMHDotDK() {
		return tblDKMHDotDK;
	}

	private Collection<TblDKMHAAOUrl> tblDKMHAAOUrls;

	public Collection<TblDKMHAAOUrl> getTblDKMHAAOUrls() {
		return tblDKMHAAOUrls;
	}

	private TblDKMHAAOUrlService tblDKMHAAOUrlService;

	private int preInsert(Message msg, TblDKMHDotDK obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postInsert(Message msg, TblDKMHDotDK obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK", "them")) {
				return "noprivilege";
			}
			tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient");
			tblCTDTDMHocKyTrongNamService = (TblCTDTDMHocKyTrongNamService) context.getBean("tbl_CTDT_DM_HocKyTrongNamClient");

			tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient");

			tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient");

			tblDKMHAAOUrlService = (TblDKMHAAOUrlService) context.getBean("tbl_DKMH_AAOUrlClient");

			msg = new Message();
			tblDKMHDotDK = new TblDKMHDotDK();
			tblDKMHDotDK.setMa(ma);
			tblDKMHDotDK.setTen(ten);
			tblDKMHDotDK.setMaLoai(maLoai);
			tblDKMHDotDK.setHocKyId(tblCTDTDMHocKyTrongNamService.gettblCTDTDMHocKyTrongNam(new Long(hocKyId)));

			DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
			Date dd = df.parse(ngayBatDau);
			GregorianCalendar c = new GregorianCalendar();
			c.setTime(dd);
			XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
			tblDKMHDotDK.setNgayBatDau(d);
			
			dd = df.parse(ngayKetThuc);
			c.setTime(dd);
			d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
			tblDKMHDotDK.setNgayKetThuc(d);

			dd = new Date();
			c.setTime(dd);
			d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
			tblDKMHDotDK.setNgayCapNhat(d);

			tblDKMHDotDK.setGhiChu(ghiChu);

			tblDKMHDotDK.setCanBoId(taiKhoanDangNhap.getCanBoId());
			
			TblDKMHDotDK dotdKTruoc = null;
			if(dotDKTruocId!=0)
			{
				dotdKTruoc = tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKTruocId));
				tblDKMHDotDK.setDotDKTruocId(dotdKTruoc);
			}

			preInsert(msg, tblDKMHDotDK);
			if (msg.isPreMessage_b()) {
				id = tblDKMHDotDKService.createtblDKMHDotDK(tblDKMHDotDK);
				//-------------------------------------
				if(dotdKTruoc!=null){
					//Lay ket qua cua dotDKtruoc dua vao dot sau
					TblDKMHKetquaService service = (TblDKMHKetquaService) context.getBean("tbl_DKMH_KetquaClient");
					service.calSPCopyKetquaDotDkHieuChinh(Integer.parseInt(id+""),dotDKTruocId);
				}
				//=====================================
				postInsert(msg, tblDKMHDotDK);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}
			tblDKMHDotDK.setId(id);

			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			{
				ConditionReport con = new ConditionReport();
				con.setCol("id");
				con.setValue1("desc");
				con.setOperator("order");
			}

			tblCTDTDMHocKyTrongNams = tblCTDTDMHocKyTrongNamService.findtblCTDTDMHocKyTrongNamByCondition(cons, 20, 1);
			tblDKMHDotDKs = tblDKMHDotDKService.findtblDKMHDotDKByCondition(cons, 20, 1);

			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Insert tbl_DKMH_DotDK Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
